package com.zbkc.main;

import java.util.ArrayList;
import java.util.List;

import com.zbkc.bean.BeifenDirectoryBean;
import com.zbkc.bean.BeifenVersionBean;
import com.zbkc.interfaces.SCInterface;
import com.zbkc.main.base.BaseTitleActivity;
import com.zbkc.util.AlertUtils;
import com.zbkc.util.BASE64Util;
import com.zbkc.util.ThreadPoolFactory;
import com.zbkc.util.ToastUtil;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class BeifenDirectoryActivity extends BaseTitleActivity implements
		AdapterView.OnItemClickListener {

	private List<BeifenDirectoryBean> dList;
	private BeifenDirectoryBean directoryBean;

	private String deviceId;
	private String directory_flag1;
	private String directory_flag2;

	private boolean isLastDirectory = false;
	private ListView listView;
	private MyAdapter myAdapter;
	private List<BeifenVersionBean> versionBeanList = new ArrayList<BeifenVersionBean>();

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				int size = dList.size();
				if (size > 0) {
					myAdapter = new MyAdapter(BeifenDirectoryActivity.this, 0,
							dList);
					listView.setAdapter(myAdapter);
					AlertUtils.dismissLoadingDialog();
				} else {
					isLastDirectory = true;
					ToastUtil.showToast(BeifenDirectoryActivity.this,
							"最后一级了，该获取版本了");
					getVersionData();
				}
				break;

			case 2:
				AlertUtils.dismissLoadingDialog();
				isLastDirectory = true;
				Intent intent = new Intent();
				intent.setClass(BeifenDirectoryActivity.this,
						BeifenFileActivity.class);
				intent.putExtra("id", deviceId);
				intent.putExtra("a", directoryBean.getDirectoryMark1());
				intent.putExtra("b", directoryBean.getDirectoryMark2());
				if (versionBeanList.size() > 0) {
					intent.putExtra("c", (versionBeanList.get(0)).getMake1());
					intent.putExtra("d", (versionBeanList.get(0)).getMake2());
				} else {
					intent.putExtra("c", "-1");
					intent.putExtra("d", "-1");
				}
				startActivity(intent);
				break;
			}
		}
	};

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_beifen_directory);
		bindView();
		getData();
	}

	private void bindView() {
		setTitle("我的备份");
		setBtn2Visiable(false);

		deviceId = getIntent().getStringExtra("id");
		deviceId = BASE64Util.encode(deviceId.getBytes());
		directory_flag1 = BASE64Util.encode("-1".getBytes());
		directory_flag2 = BASE64Util.encode("-1".getBytes());

		listView = ((ListView) findViewById(R.id.listView));
		listView.setOnItemClickListener(this);
	}

	private void getData() {
		AlertUtils.showLoadingDialog(this, "正在获取数据,请稍后", true);
		ThreadPoolFactory.getInstance().execute(new Runnable() {
			public void run() {
				dList = SCInterface.getBeifenDirectoryList(deviceId,
						directory_flag1, directory_flag2);
				handler.sendEmptyMessage(1);
			}
		});
	}

	private void getVersionData() {
		ThreadPoolFactory.getInstance().execute(new Runnable() {
			public void run() {
				versionBeanList = SCInterface.getBeifenVersionList(deviceId,
						directoryBean.getDirectoryMark1(),
						directoryBean.getDirectoryMark2());
				handler.sendEmptyMessage(2);
			}
		});
	}

	public void onItemClick(AdapterView<?> paramAdapterView, View paramView,
			int paramInt, long paramLong) {
		if (!isLastDirectory) {
			directoryBean = ((BeifenDirectoryBean) dList.get(paramInt));
			directory_flag1 = directoryBean.getDirectoryMark1();
			directory_flag2 = directoryBean.getDirectoryMark2();
			getData();
		}
	}

	class MyAdapter extends ArrayAdapter<BeifenDirectoryBean> {
		public MyAdapter(Context context, int textViewResourceId,
				List<BeifenDirectoryBean> objects) {
			super(context, textViewResourceId, objects);
		}

		public View getView(int paramInt, View paramView,
				ViewGroup paramViewGroup) {
			ViewHolder viewHolder;
			if (paramView == null) {
				viewHolder = new ViewHolder();
				paramView = View.inflate(getContext(),
						R.layout.listview_beifen_item, null);
				viewHolder.tvtitle = ((TextView) paramView
						.findViewById(2131296257));
				paramView.setTag(viewHolder);
			} else {
				viewHolder = ((ViewHolder) paramView.getTag());
			}
			viewHolder.tvtitle
					.setText(((BeifenDirectoryBean) getItem(paramInt))
							.getDirectoryName());
			return paramView;
		}

		class ViewHolder {
			TextView tvtitle;
		}
	}

}
